<template>
  <div @keyup.enter="getData()">
    <el-form class="query-table-form" :inline="true">
      <el-form-item>
        <el-input v-model="params.customer_name" placeholder="项目名称查询" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="params.corp_name" placeholder="负责人查询" :clearable="true"></el-input>
      </el-form-item>
      <el-form-item>
        <select-address
          v-model="params.address_code"
          clearable
          placeholder="地址查询"
          :auto-top="true"
          @change="getData()"
        ></select-address>
      </el-form-item>
      <el-form-item>
        <c-select
          :options="enumGuaType"
          v-model="params['gua_type.eq']"
          clearable
          placeholder="业务模式"
          @change="getData()"
        ></c-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="getData()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button
          icon="el-icon-download"
          :loading="exportLoading"
          @click="exportExcel('项目立项')"
        >
          导出
        </el-button>
      </el-form-item>
    </el-form>
    <c-table border v-loading="loading" :columns="tableColumns" :data="tableData">
      <template v-slot:address="scope">
        <div class="text-truncate" :title="scope.row.address">{{ scope.row.address }}</div>
      </template>
      <template v-slot:name="scope">
        <el-popover placement="top-start" title="项目经理" trigger="hover">
          <div>
            <div>{{ `A角： ${scope.row.res_a_name || '-'}；` }}</div>
            <div class="mb-2">{{ `电话： ${scope.row.res_a_phone || '-'}` }}</div>
            <div>{{ `B角： ${scope.row.res_b_name || '-'}；` }}</div>
            <div>{{ `电话： ${scope.row.res_b_phone || '-'}` }}</div>
          </div>
          <div slot="reference">
            <div class="status-label badge-info text-left w-100">
              <div>A角:{{ scope.row.res_a_name }}</div>
              <div>B角:{{ scope.row.res_b_name }}</div>
            </div>
          </div>
        </el-popover>
      </template>
      <template v-slot:letter="scope">
        <div
          class="badge cursor-pointer"
          :class="`badge-${scope.row.letterStatusStyle}`"
          @click="showLetter(scope.row)"
        >
          <div v-if="+scope.row.confuse_status === 3" :title="scope.row.confuse_des">
            {{ scope.row.letterStatusLabel }}
          </div>
          <span v-else>{{ scope.row.letterStatusLabel }}</span>
        </div>
      </template>
    </c-table>
    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="totalCount"
        @current-change="getData"
      ></el-pagination>
    </div>

    <view-dialog :visible.sync="viewVisible" :related-data="viewRow"></view-dialog>
    <handler-project-dialog
      :visible.sync="approveVisible"
      :query-id="relatedId"
      @success="reGetData"
    ></handler-project-dialog>

    <el-dialog :visible.sync="letterVisible" width="840px">
      <letter-view :query-id="letterQueryId"></letter-view>
    </el-dialog>
  </div>
</template>

<script>
import { enumItemStatus, enumProcessStatus } from '@/credit/plugins/enumTown'
import { baseTableColumns } from '@/credit/views/county/unionProvince/Approve'
import baseMixins from '@/bigcredit/views/self/fast/mixins'

import SelectAddress from '@/components/select/SelectAddress'
import ViewDialog from '@/bigcredit/views/self/fast/item/ViewDialog'
import HandlerProjectDialog from '@/bigcredit/views/self/fast/item/components/HandlerProjectDialog'
import LetterView from '@/credit/views/county/unionProvince/components/LetterView'
import { enumGuaType } from '@/bigcredit/views/program/enum'
export const enumLetterStatus = {
  '-1': { label: '未发函', style: 'warning' },
  0: { label: '未签章', style: 'info' },
  1: { label: '已签章', style: 'success' },
  2: { label: '已签章', style: 'success' },
  3: { label: '已作废', style: 'danger' },
}
export default {
  components: {
    SelectAddress,
    ViewDialog,
    HandlerProjectDialog,
    LetterView,
  },
  mixins: [baseMixins],
  data() {
    return {
      enumGuaType,
      order: {
        N1is_history: 'asc',
        N2item_status: 'asc',
        N3item_date: 'desc',
        N4create_time: 'desc',
      },
      params: {
        index: 1,
        size: 15,
        address_code: '',
        customer_name: '',
        corp_name: '',
        'is_approval.eq': '1',
        subKey: 'ele_letter_info.customer_related_id',
      },

      tableData: [],
      tableColumns: [
        ...baseTableColumns,
        {
          attrs: { label: '合作银行', prop: 'bank_name', align: 'center' },
          process: v => v || '-',
        },
        {
          attrs: { label: '担保意向函', prop: 'letterStatusLabel', align: 'center' },
          slot: 'letter',
        },
        { attrs: { label: '项目经理', prop: 'resName', width: 100 }, slot: 'name' },
        {
          attrs: { label: '待处理人员', prop: 'itemPerson', width: 80, align: 'center' },
        },
        {
          attrs: { label: '操作', align: 'center', width: 100 },
          operations: [
            {
              content: '项目处理',
              icon: 'fa fa-gavel',
              event: this.handleEdit,
              ifRender: row => row.editable,
            },
            { content: '查看', icon: 'fa fa-search', event: this.handleView },
          ],
        },
      ],

      viewVisible: false,
      viewRow: {},
      enumParams: {
        0: {
          'item_status.eq': 0,
        },
        1: {
          'item_status.eq': 1,
        },
        2: {
          'item_status.eq': 2,
        },
      },
      selectStatus: '',
      approveVisible: false,
      relatedId: '',
      letterVisible: false,
      letterQueryId: '',
    }
  },
  computed: {},
  watch: {},
  mounted() {},
  created() {
    this.getData()
  },
  methods: {
    showLetter(row) {
      if (row.docId) {
        this.letterVisible = true
        this.letterQueryId = row.customer_related_id
      }
    },
    async reGetData() {
      this.getData()
      this.$store.dispatch('getTaskCountBusSelfFast')
    },
    handleEdit(row) {
      this.approveVisible = true
      this.relatedId = row.customer_related_id || ''
    },
    dataAdapter(item) {
      item.guaType = enumGuaType[item.gua_type]
      item.itemPerson =
        +item.item_status === 0 && +item.confuse_status !== 3
          ? `${item.res_a_name} | ${item.res_b_name}`
          : '已完成'

      item.prelimTime = this.$utils.dateFormat(item.item_date, this.dateFmt)
      item.resName = `A角: ${item.res_a_name} B角: ${item.res_b_name}`
      const itemStatus = enumItemStatus[item.item_status] || {}
      item.itemStatusLabel = itemStatus.label
      item.itemStatusStyle = itemStatus.css
      item.processLabel = enumProcessStatus[item.process_status] || ''

      item.item_status = +item.item_status
      item.hasItem = item.item_status === 1

      const letters = (item?.ele_letter_info ?? []).filter(item => +item.ele_letter_type === 1)
      const normalLetter = letters.sort((a, b) => a.sign_status - b.sign_status)
      item.letterStatus = normalLetter?.[0]?.sign_status ?? -1
      const letterStatusObj = enumLetterStatus[item.letterStatus] || {}
      item.docId = normalLetter?.[0]?.doc_id || ''
      item.letterStatusLabel = letterStatusObj.label
      item.letterStatusStyle = letterStatusObj.style
      item.editable =
        [item.res_a, item.res_b, item.res_user_id_risk_point].includes(this.userId) &&
        +item.confuse_status !== 3

      if (+item.confuse_status === 3) {
        item.letterStatusLabel = '已拒绝'
        item.letterStatusStyle = 'danger'
      }
      return item
    },
    handleView(row) {
      this.viewRow = row
      this.viewVisible = true
    },
    handleStatusChange() {
      const stautsParams = this.enumParams[this.selectStatus] || {}
      const resetParams = Object.keys(this.enumParams).reduce((prev, curr) => {
        for (const key in this.enumParams[curr]) {
          prev[key] = ''
        }
        return prev
      }, {})
      this.params = {
        ...this.params,
        ...resetParams,
        ...stautsParams,
      }
      this.getData()
    },
  },
}
</script>
<style lang="scss" scoped>
.status-label {
  display: inline-block;
  padding: 0.25em 0.4em;
  font-size: 75%;
  font-weight: 700;
  line-height: 1;
  text-align: center;
  vertical-align: baseline;
  border-radius: 0.25rem;
  transition: color 0.15s;
}
</style>
